package com.elm.service.impl;

import com.elm.mapper.UserMapper;
import com.elm.po.User;
import com.elm.service.UserService;
import com.elm.util.JwtUtil;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List; 

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HttpServletRequest request;

    private String getCurrentUserId() {
        String token = (String) request.getAttribute("token");
        return JwtUtil.getUserIdFromToken(token);
    }

    private String getCurrentUserType() {
        String token = (String) request.getAttribute("token");
        return JwtUtil.getUserTypeFromToken(token);
    }

    @Override
    public User getUserByIdByPass(String userId, String password) {
        // 登录验证，公开接口
        return userMapper.getUserByIdByPass(userId, password);
    }

    @Override
    public User getUserById(String userId) {
        // 检查用户是否存在，公开接口
        return userMapper.getUserById(userId);
    }

    @Override
    public int createUser(User user) {
        // 注册用户，公开接口（需校验userId唯一性）
        if (getUserById(user.getUserId())!=null) {
            return 0; // 用户名已存在
        }
        user.setUserType("user"); // 默认普通用户
        user.setDelTag(0); // 未删除
        return userMapper.createUser(user);
    }

    @Override
    public int updateUser(User user) {
        // 仅允许修改自己的信息
        String currentUserId = getCurrentUserId();
        if (!user.getUserId().equals(currentUserId)) {
            throw new RuntimeException("无权限：只能修改自己的信息");
        }
        return userMapper.updateUser(user);
    }

    @Override
    public int updatePassword(String oldpassword, String newpassword) {
        String userId = getCurrentUserId();
        // 验证旧密码
        User user = userMapper.getUserByIdByPass(userId, oldpassword);
        if (user == null) {
            return 0; // 旧密码错误
        }
        return userMapper.updatePassword(userId, newpassword);
    }

    // Admin专属：封禁用户
    @Override
    public int updateUserStatus(String targetUserId) {
        if (!"admin".equals(getCurrentUserType())) {
            throw new RuntimeException("权限不足：仅管理员可封禁用户");
        }
        // 封禁用户（更新status字段）
        return userMapper.updateUserStatus(targetUserId);
    }

    // 商家Boss专属：添加员工
    @Override
    public int addStaff(String staffUserId) {
        String bossId = getCurrentUserId();
        if (!"boss".equals(getCurrentUserType())) {
            throw new RuntimeException("权限不足：仅商家老板可添加员工");
        }
        // 获取boss所属商家
        User boss = userMapper.getUserById(bossId);
        Integer businessId = boss.getBusinessId();
        if (businessId == null) {
            throw new RuntimeException("错误：老板未关联商家");
        }
        // 将员工关联到该商家，并设置用户类型为staff
        return userMapper.addStaff(staffUserId,businessId);
    }
 
    @Override
    public int deleteStaff(String staffUserId) {
        String bossId = getCurrentUserId();
        if (!"boss".equals(getCurrentUserType())) {
            throw new RuntimeException("权限不足：仅商家老板可添加员工");
        }
        // 获取boss所属商家
        User boss = userMapper.getUserById(bossId);
        Integer businessId = boss.getBusinessId();
        if (businessId == null) {
            throw new RuntimeException("错误：老板未关联商家");
        }
        // 将员工关联到该商家，并设置用户类型为staff
        return userMapper.deleteStaff(staffUserId,businessId);
    }
    
    @Override
    public List<User> getAllStaff() {
        String bossId = getCurrentUserId();
        if (!"boss".equals(getCurrentUserType())) {
            throw new RuntimeException("权限不足：仅商家老板可添加员工");
        }
        // 获取boss所属商家
        User boss = userMapper.getUserById(bossId);
        Integer businessId = boss.getBusinessId();
        if (businessId == null) {
            throw new RuntimeException("错误：老板未关联商家");
        }
        // 将员工关联到该商家，并设置用户类型为staff
        return userMapper.getAllStaff(businessId);
    }
    
    
    
}